package hn.sigit.dao.hnd.administrative;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.jboss.seam.Component;

import hn.sigit.dao.SigitDAO;
import hn.sigit.model.hnd.administrative.HND_TransactionLog;

public class HND_TransactionLogDAO extends SigitDAO<HND_TransactionLog> {
	private static final long serialVersionUID = 1L;

	public static HND_TransactionLog loadLastTransactionLogByPresentationId(Long presentationId) {
		return loadLastTransactionLogByPresentationId(getSession(), presentationId);
	}
	public static List<HND_TransactionLog> loadTransactionLogsByPresentationId(Long presentationId) {
		Session commonsDatabase = (Session)Component.getInstance(COMMONS_DB);
		return loadTransactionLogsByPresentationId(commonsDatabase, presentationId);
	}
	
	public static HND_TransactionLog loadLastTransactionLogByPresentationId(
			Session session, Long presentationId) {
		Long maxId = (Long) session.createCriteria(HND_TransactionLog.class)
		.setProjection(Projections.max("id"))
		.createCriteria("transaction").add(Restrictions.eq("presentationId", presentationId))
		.uniqueResult();
		
		
		Criteria criteria = session.createCriteria(HND_TransactionLog.class);
		
		return (HND_TransactionLog) criteria.add(Restrictions.eq("id", maxId))
		.createCriteria("transaction").add(
				Restrictions.eq("presentationId", presentationId)).uniqueResult();
	}
	@SuppressWarnings("unchecked")
	public static List<HND_TransactionLog> loadTransactionLogsByPresentationId(Session session, Long presentationId) {
		Criteria criteria = session.createCriteria(HND_TransactionLog.class);
		criteria.addOrder(Order.asc("id"));
		
		return criteria.createCriteria("transaction").add(
				Restrictions.eq("presentationId", presentationId)).list();
	}
}
